//
//  AKCustomNaviBar.m
//  MXDK
//
//  Created by chen on 2018/3/20.
//  Copyright © 2018年 chen. All rights reserved.
//

#import "AKCustomNaviBar.h"

@implementation AKCustomNaviBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.titleLabel = [QMUILabel new];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.adjustsFontSizeToFitWidth = true;
        self.titleLabel.numberOfLines = 2;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.userInteractionEnabled = YES;
        self.titleLabel.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        self.titleLabel.minimumScaleFactor = 0.6;
        [self.titleLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTappedTitle)]];
        [self addSubview:self.titleLabel];
        //
        self.leftButton = [QMUIButton new];
        [self.leftButton addTarget:self action:@selector(didClickedLeftButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.leftButton];
        //
        self.rightButton = [QMUIButton new];
        self.rightButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        [self.rightButton addTarget:self action:@selector(didClickedRightButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.rightButton];
        
        
        //
        CGFloat centerYOffset = 0.5 * Height_StatusBar;
        //
        [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(centerYOffset);
            make.left.equalTo(self).offset(5);
        }];
        //
        [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(centerYOffset);
            make.right.equalTo(self).offset(-10.0);
        }];
        //
//        self.titleLabel.backgroundColor = [UIColor orangeColor];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).offset(centerYOffset);
            make.height.mas_equalTo(self).offset(-Height_StatusBar);
            make.width.mas_lessThanOrEqualTo(self).offset(-120.0);
        }];
        
        self.titleLeftNearView = self.leftButton;
        self.titleRightNearView = self.rightButton;
    }
    return self;
}

- (void)didTappedTitle {
    if (self.didTappedNaviTitle) {
        self.didTappedNaviTitle();
    }
}

- (void)didClickedLeftButton {
    if (self.didClickedNaviLeftButton) {
        self.didClickedNaviLeftButton();
    }
}

- (void)didClickedRightButton {
    if (self.didClickedNaviRightButton) {
        self.didClickedNaviRightButton();
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //
    CGFloat width = CGRectGetWidth(self.frame);
    //
    CGFloat leftMaxWidth = CGRectGetMaxX(self.titleLeftNearView.frame);
    CGFloat rightMaxWidth = width-CGRectGetMinX(self.titleRightNearView.frame);
    //
    CGFloat maxWidth = MAX(leftMaxWidth, rightMaxWidth);
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_lessThanOrEqualTo(self).offset(-maxWidth*2);
    }];
}



#pragma mark - setter
/**
 设置背景颜色时判断亮色 修改leftbutton的tintColor
 */
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    if ([backgroundColor qmui_colorIsDark]) {
        self.leftButton.tintColor = self.rightButton.tintColor = RGBHEX(0xFFFFFF);
        [self setShadowOpen:false];
        self.titleLabel.textColor = RGBHEX(0xFFFFFF);
        [self.rightButton setTitleColor:self.titleLabel.textColor forState:UIControlStateNormal];
    }else {
        self.leftButton.tintColor = self.rightButton.tintColor = RGBHEX(0x222222);
        [self setShadowOpen:true];
        self.titleLabel.textColor = [UIColor blackColor];
        [self.rightButton setTitleColor:self.titleLabel.textColor forState:UIControlStateNormal];
    }
}

- (void)setShadowOpen:(BOOL)open {
    if (open) {
        self.layer.shadowOpacity = 1;   
        self.layer.shadowColor = RGBHEXA(0x000000, 0.05).CGColor;
        self.layer.shadowRadius = 10.0;
        self.layer.shadowOffset = CGSizeMake(0, 1);
    }else {
        self.layer.shadowOpacity = 0;
    }
}



@end
